﻿/* 
	Author	: Dale McKay
	Email	: dalesmckay@gmail.com
	Purpose	: To inject the statistical graphs from TWStats on the Rankings and Tribe Member Screens.
	Credit	: Based on a concept by an unKnown author.
	Todo	:
	Notes	:
	

	######################
	Client Launcher (live):
	######################
	
javascript:
var config={
	showPoints:true,
	showVillages:true,
	showODA:true,
	showODD:true,
	width:'180px',
	height:'96px'
};
(window.main||self).$.getScript('http://dl.dropbox.com/u/25377948/twscripts/ranking_stats.js',function(){var script=new RankingStats();script.execute(config);});void(0);

____________________________________________________________

Copyright (C) 2011 Dale McKay, all rights reserved
version 1.0, 4 May 2011

This software is provided 'as-is', without any express or implied warranty. In no event will the author be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
This notice may not be removed or altered from any source distribution.
____________________________________________________________
*/

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Class: RankingStats
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

worldnames = {
  "ae1":"عالم 1",
  "ae2":"عالم 2",
  "ae3":"عالم 3",
  "ae4":"عالم 4",
  "ae5":"عالم 5",
  "ae6":"عالم 6",
  "ae7":"عالم 7",
  "ae8":"عالم 8",
  "ae9":"عالم 9",
  "ae10":"عالم 10",
  "ae11":"عالم 11",
  "ae12":"عالم 12",
  "ae13":"عالم 13",
  "ae14":"عالم 14",
  "ae15":"عالم 15",
  "ae16":"عالم 16",
  "ae17":"عالم 17",
  "ae18":"عالم 18",
  "ae19":"عالم 19",
  "ae20":"عالم 20",
  "ae21":"عالم 21",
  "ae22":"عالم 22",
  "ae23":"عالم 23",
  "ae24":"عالم 24",
  "ae25":"عالم 25",
  "aec1":"عالم التحدي",
  "aes1":"عالم السرعة",
  "aep1":"العالم المعمر",  
};

screennames = {
  "ranking":"الترتيب",
};

modenames = {
  "player":"اللاعبين",
  "ally":"القبائل",
  "con_player":"اللاعبين بالقارة",
  "con_ally":"القبائل بالقارة",
  "kill_player":"الوحدات المهزومة(اللاعبين)",
  "kill_ally":"الوحدات المهزومة (القبائل)",  
  "awards":"الإنجازات",
  "wars":"الحروب",
};




function RankingStats(){
	var win=(window.main||self),$=win.$;

	var script={
		id:'32FC2DD8-3B1B-11E1-A8FA-F9F94824019B',
		name:'سكربت الترتيب (مركز الإحصائيات)',
		version:1.05,
		minGameVersion:7.00,
		author:{
			name:'تم برمجة السكربت بواسطة dalesmckay',
			email:'dalesmckay@gmail.com',
			url:'http://www.crosstrigger.com',
		},
		credit:'(إستناد إلى: <span style="color:blue;">مصدر غير معروف</span>)',
		runOnce:true
	};

	// ........................................................................
	function fnInt(value){return parseInt(value||'0',10);}
	// ........................................................................
	function fnPrint(msg){
		var myself=this;
	
		if($('#'+script.id).length<=0){
			$('body').append('<div id="'+script.id+'"></div>');
		}
	
		$('#'+script.id).append('<span>'+msg+'</span><br/>');
	} // fnPrint
	// ........................................................................
	function fnPrintVersion(){
		var authorURL=script.author.url?('<a href="'+script.author.url+'" target="_blank">'+script.author.name+'</a>'):script.author.name;
		
		fnPrint("=========================");
		fnPrint(authorURL + "'s " + script.name + ": v" + script.version.toFixed(2) + (script.credit?('<br/>'+script.credit):''));
		fnPrint("=========================");
		fnPrint($.trim($('.server_info').text().match(/\|\s*(.+)/)[1]));
		fnPrint("الحساب: "+win.game_data.player.name);
		fnPrint("الحضانة : "+((fnInt(win.game_data.player.sitter_id)>0)?("نعم - "+win.game_data.player.sitter_id):"لا"));
		fnPrint("=========================");
		fnPrint("الإصدار: "+win.game_data.version);
		fnPrint("العالم  : "+worldnames[win.game_data.world]);
		fnPrint("الشاشة : "+screennames[win.game_data.screen]);
		fnPrint("النوع   : "+modenames[win.game_data.mode]);
		fnPrint("=========================");
	} // fnPrintVersion
	// ........................................................................
	function fnInjectStatGraphs(config){
		var url=('http://%%MARKET.twstats.com').replace(/\%\%MARKET/ig,(win.game_data.market=='en')?'www':win.game_data.market)+'/image.php';
		var template='%%POINTS%%VILLAGES%%ODA%%ODD';
		var cellTemplate='<td class="dsm%%MODE"%%VISIBLE><img src="%%URL?type=%%TYPEgraph&graph=%%MODE&%%ID&s=%%WORLD" style="width:%%WIDTH;height:%%HEIGHT;"></img></td>';
		var mode=(win.location.href.match(/mode\=(\w+)/i)||[null,'player'])[1];
		var table=$('#'+mode.replace(/awards/i,'award')+'_ranking_table'+(mode.match(/kill\_/i)?'~table:eq(0)':''));
		var srcHTML;
	
		if(table.length<=0){
			table=$('table[class*="vis"]:has(td a[href*="info_player"])');
		}
		if(table.length<=0){
			return;
		}

		// Inject the Checkboxes	
		srcHTML='';
		srcHTML+='<div id="dsmStats">';
		srcHTML+=	'<input id="cbPoints" type="checkbox" value="dsmpoints"'+(config.showPoints?' checked':'')+'/><label for="cbPoints">إحصائيات النقاط</label>';
		srcHTML+=	'<input id="cbVillages" type="checkbox" value="dsmvillages"'+(config.showVillages?' checked':'')+'/><label for="cbVillages">إحصائيات القرى</label>';
		srcHTML+=	'<input id="cbODA" type="checkbox" value="dsmoda"'+(config.showODA?' checked':'')+'/><label for="cbODA">إحصائيات كأفضل مهاجم</label>';
		srcHTML+=	'<input id="cbODD" type="checkbox" value="dsmodd"'+(config.showODD?' checked':'')+'/><label for="cbODD">إحصائيات كأفضل مدافع</label>';
		srcHTML+='</div>';
		
		$(srcHTML).insertBefore(table);
		$('#cbPoints').click(function(){$(this).is(':checked')?$('.'+this.value).show('fast'):$('.'+this.value).hide('fast')});
		$('#cbVillages').click(function(){$(this).is(':checked')?$('.'+this.value).show('fast'):$('.'+this.value).hide('fast')});
		$('#cbODA').click(function(){$(this).is(':checked')?$('.'+this.value).show('fast'):$('.'+this.value).hide('fast')});
		$('#cbODD').click(function(){$(this).is(':checked')?$('.'+this.value).show('fast'):$('.'+this.value).hide('fast')});

		// Inject the Column Headers
		srcHTML=template
			.replace(/\%\%POINTS/ig,'<th class="dsmpoints"%%VISIBLE>إحصائيات النقاط</th>')
			.replace(/\%\%VILLAGES/ig,'<th class="dsmvillages"%%VISIBLE>إحصائيات القرى</th>')
			.replace(/\%\%ODA/ig,'<th class="dsmoda"%%VISIBLE>إحصائيات كأفضل مهاجم</th>')
			.replace(/\%\%ODD/ig,'<th class="dsmodd"%%VISIBLE>إحصائيات كأفضل مدافع</th>')
		;
		var hdr=table.find('tr:eq(0)');
		hdr.html(hdr.html()+srcHTML);

		// Inject the Cells.
		srcHTML=template
			.replace(/\%\%POINTS/ig,config.showPoints?cellTemplate.replace(/\%\%MODE/ig,'points'):'').replace(/\%\%VISIBLE/ig,config.showPoints?'':' style="display:none;"')
			.replace(/\%\%VILLAGES/ig,config.showVillages?cellTemplate.replace(/\%\%MODE/ig,'villages'):'').replace(/\%\%VISIBLE/ig,config.showVillages?'':' style="display:none;"')
			.replace(/\%\%ODA/ig,config.showODA?cellTemplate.replace(/\%\%MODE/ig,'oda'):'').replace(/\%\%VISIBLE/ig,config.showODA?'':' style="display:none;"')
			.replace(/\%\%ODD/ig,config.showODD?cellTemplate.replace(/\%\%MODE/ig,'odd'):'').replace(/\%\%VISIBLE/ig,config.showODD?'':' style="display:none;"')
			.replace(/\%\%URL/ig,url)
			.replace(/\%\%WORLD/ig,win.game_data.world)
			.replace(/\%\%TYPE/ig,mode.match(/ally/i)?'tribe':'player')
			.replace(/\%\%WIDTH/ig,config.width)
			.replace(/\%\%HEIGHT/ig,config.height)
		;
		table.find('tr:gt(0)'+(win.game_data.screen=='ally'?':has(td a[href*="info_player"])':'')).each(function(i,e){
			var id=($(e).find('a:eq(0)').attr('href')||'').match(/id\=\d*/i);
			e.innerHTML+=srcHTML.replace(/\%\%ID/ig,id);
			
			// Print the first row of TWStats URLs as a debug sample.
			if(!i){
				fnPrint('<br/><span style="font-weight:bold;">روابط بسيطة :</span>');
				$(srcHTML.replace(/\%\%ID/ig,id)).find('img').each(function(i,e){
					fnPrint('<a style="margin-left:1em;" href="' + e.src + '" target="_blank">' اضغط هنا '</a>');
				});
			}
		});
	} // fnInjectStatGraphs
	// ........................................................................


	return{
		execute:function(config){
			if($('#dsmStats').length>0){
				return;
			}
			
			fnPrintVersion();

			var url=win.location.href;

			if((url.match(/screen\=ally/i)&&url.match(/mode\=members/i))||(['ranking','info_member'].indexOf(win.game_data.screen)>=0)){
				fnInjectStatGraphs(config);
			}
			else if(confirm('يجب أن يتم تشغيل هذا السكربت من :\n\t* شاشة الترتيب\n\t* شاشة ترتيب القبائل\n\nالذهاب لصحفة الترتيب الآن؟')){
				if(url.match(/screen\=ally/i)){
					win.location=win.game_data.link_base_pure.replace(/screen\=\w*/i,'screen=ally&mode=members');
				}
				else{
					win.location=win.game_data.link_base_pure.replace(/screen\=\w*/i,'screen=ranking');
				}
			}
		}
	};
} // RankingStats::constructor
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 